<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			function show(){
				//var _uid=uid.value;
				//0.接收地址栏的uid的值
				//0.1.获取地址栏中的查询字符串对象
				var obj=new URLSearchParams(location.search);
				//0.2.通过这个对象的get方法获取对应的参数
				var _uid=obj.get("uid");
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						var arr=JSON.parse(r);
						uname.value=arr[0].uname
						all_uname=arr[0].uname
						email.value=arr[0].email
						phone.value=arr[0].phone
						user_name.value=arr[0].user_name
						//console.log(arr[0].gender)
						if(arr[0].gender==1){
							man.checked=true;
						}else if(arr[0].gender==0){
							woman.checked=true;
						}else{
							unk.checked=true;
						}
					}
				}
				xhr.open("get",`/ajax/v1/search/${_uid}`,true)
				xhr.send();
			}
			function update(){
				if(uname_msg.innerHTML==""){
					
				}else if(uname_msg.innerHTML!="√"){
					alert("用户名重复");
					return;
				}
				//0.接收地址栏的uid的值
				//0.1.获取地址栏中的查询字符串对象
				var obj=new URLSearchParams(location.search);
				//0.2.通过这个对象的get方法获取对应的参数
				var _uid=obj.get("uid");
				var _uname=uname.value;
				var _email=email.value;
				var _phone=phone.value;
				var _user_name=user_name.value;
				var _gender="-1";
				if(man.checked){
					_gender="1"
				}else if(woman.checked){
					_gender="0"
				}else{
					_gender="-1"
				}
				//var _uid=uid.value;
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						if(r==1){
							alert("修改成功")
							location.href="2_list.html"
						}else{
							alert("修改失败")
						}
					}
				}
				xhr.open("put",`/ajax/v1/update`,true);
				
				var formdata=`uid=${_uid}&uname=${_uname}&email=${_email}&phone=${_phone}&user_name=${_user_name}&gender=${_gender}`
				xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
				xhr.send(formdata);
			}
			function getu(){
				var _uname=uname.value;
				if(!_uname){
					uname_msg.innerHTML="用户名不能为空";
				}
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						if(r==1){
							if(_uname==all_uname){
								uname_msg.innerHTML="√";
							}else{
								uname_msg.innerHTML="用户名重复";
								return;
							}
						}else{
							uname_msg.innerHTML="√"
						}
					} 
				}
				xhr.open("get",`/ajax/v1/getUname/${_uname}`,true)
				xhr.send();
				}
		</script>
	</head>
	<body onload="show()">
		<!-- <input type="text" id="uid"> -->
		<!-- <button onclick="show()">查询</button> -->
		<!-- <br> -->
		<input type="text" id="uname" onblur="getu()">
		<span id="uname_msg">
			
		</span><br>
		<input type="text" id="email"><br>
		<input type="text" id="phone" ><br>
		<input type="text" id="user_name" ><br>
		<input type="radio" name="gender" id="man">
		<label for="man">男</label>
		<input type="radio" name="gender" id="woman">
		<label for="woman">女</label>
		<input type="radio" name="gender" id="unk">
		<label for="unk">保密</label><br>
		<button onclick="update()">修改</button>
	</body>
</html>
